import { mapMutations, mapGetters, mapActions } from 'vuex'
export default {
    computed: {
        ...mapGetters({
            getChild: 'getAllChildNodeByParent',
            copyData: 'getCopyData'
        }),
        pasteLimit() {
            return this.copyData.data.length == 0
        }
    },
    data() {
        return {
            tempDelList: []     //暂时保存剪切的id(只有粘贴后才算剪切原有数据)
        }
    },
    methods: {
        ...mapMutations(['setCopyData']),
        ...mapActions(['delPlanTable', 'addNodeOfPlanTable']),
        copy(flag) {
            const self = this;
            let allActionList = []
            this.tempDelList = []
            //得到赋值节点的父节点
            let actionList = self.$store.state.planTable.hasSelectData.length == 0 ? 
                [self.menuRecord]: self.$store.state.planTable.hasSelectData;
            //得到所有需要赋值的节点
            actionList.forEach(ele => {
                //将复制或者剪切的最顶级节点和末尾节点加入allActionList
                if (!actionList.some(data => ele.parentIds.includes(data.id))) {
                  if (flag == '剪切') 
                    this.tempDelList.push(ele.oid)
                  allActionList.push(ele)
                  allActionList.push(...self.getChild(ele))
                }
            })
            this.setCopyData({
                data: allActionList,
                flag: flag
            })
        },
        //如果节点的id改变了且有子节点，则将子节点的parentIds修改为新的父节点id
        setCopyDataChild(newId,rowId,list) {
            list.forEach(item => {
                const index = item.parentIds.findIndex(sub => sub == rowId)
                index != -1 && (item.parentIds[index] = newId)
            })
        },
        paste() {
            const self = this;
            self.$store.state.planTable.shearId = self.tempDelList
            let addIndex = this.showSource.filter(item => this.isAddNode(item.id)).length
            let params = JSON.parse(JSON.stringify(self.copyData.data))
            params.forEach(item => {
                const newId = `PIProjectManager.model.Task-${addIndex}`
                self.setCopyDataChild(newId, item.id, params)
                //找到第一个新增节点id的下标,并删除下标前的id
                const splicIndex = item.parentIds.findIndex(par => par.toString().indexOf('PIProjectManager.model.Task') != -1)
                item.parentIds.splice(0, splicIndex == -1 ? item.parentIds.length : splicIndex)
                item.id = newId;
                item.oid = newId;
                item.taskId = newId;
                item.rowId = newId;
                item.deliverable = []
                item.healthStatusType = 'UNAVAILABLE'
                item.plannableStatus = 'SCHEDULED'
                item.parentIds = [...self.menuRecord.parentIds, ...item.parentIds]
                addIndex++;
            })
            //点击节点的位置
            const clickIndex = self.showSource.findIndex(item => item.id == self.menuRecord.id);
            //点击节点的子节点数量
            const childLength = self.getChild(self.menuRecord).length
            //在节点的下一个同级位置粘贴
            self.addNodeOfPlanTable({
                target: params,
                targetIndex: clickIndex + childLength + 1
            })
            //给父节点设置最早开始时间和最晚结束时间
            let strParentIds = ''
            for (let index = 0, len = params.length; index < len; index++) {
              let target = params[index]
              //只需给让一个同级节点设置即可
              if (this.isLastChildNode(target) && JSON.stringify(target.parentIds) != strParentIds) {
                strParentIds = JSON.stringify(target.parentIds)
                //设置父节点的最早开始时间和最晚结束时间
                this.setMinMaxDate(target, this.$store.state.planTable.planTable, true)
              }
            }
            //移除表格中剪切的元素
            if (self.copyData.flag == '剪切') {
                this.$store.state.planTable.shearId.forEach(rowId => {
                    let id = ''
                    const startIndex = self.$store.state.planTable.planTable.findIndex(item => {
                        if (item.oid == rowId)
                            id = item.id
                        return item.oid == rowId
                    })
                    const childeLength = self.getChild({
                      id
                    }).length
                    //将要删除的当前节点
                    const currentNode = self.showSource.find(item => item.id == id)
                    self.$store.state.planTable.planTable.splice(startIndex, childeLength + 1)
                    //修改任务结构后修改父节点的状态
                    //当前节点父节点
                    let parentNode = this.showSource.find(item =>
                      item.id == currentNode.parentIds[currentNode.parentIds.length - 1])
                    if (this.isLastChildNode(parentNode, false)) {
                        parentNode.activityType = 'Task'
                    }else {
                        parentNode.activityType = 'WBS'
                        parentNode.resource = ''
                        parentNode.reviewerReference = ''
                    }
                    this.setRowOfPlanTable(parentNode)
                })
                self.setCopyData({
                    data: [],
                    flag: '复制'
                })
            }
        }
    }
}